package com.bsoft.hospitalchs.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.adapter.ChildCaseAdapter;
import com.bsoft.hospitalchs.adapter.SearchAdapter;
import com.bsoft.hospitalchs.base.BaseActivity;
import com.bsoft.hospitalchs.model.SearchBean;
import com.bsoft.hospitalchs.model.SearchListReq;
import com.bsoft.hospitalchs.model.SearchResBean;
import com.bsoft.hospitalchs.model.SearchResList;
import com.bsoft.hospitalchs.ui.child.ChildInfoActivity;
import com.bsoft.hospitalchs.widget.CustomProgress;
import com.google.gson.Gson;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 泅渡者
 * Created on 2017/5/3.
 */

public class HomeActivity extends BaseActivity<HomeContract.Presenter> implements HomeContract.View, PullLoadMoreRecyclerView.PullLoadMoreListener {
    @Bind(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView pullLoadMoreRecyclerView;
    @Bind(R.id.edt_search)
    EditText edtSearch;
    @Bind(R.id.btn_search)
    Button btnSearch;
    @Bind(R.id.ll_back)
    LinearLayout llback;
    @Bind(R.id.iv_head)
    ImageView ivHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.textView2)
    TextView textView2;
    @Bind(R.id.tv_gender)
    TextView tvGender;
    @Bind(R.id.tv_account)
    TextView tvAccount;
    @Bind(R.id.tv_yue)
    TextView tvYue;
    @Bind(R.id.tv_doctor)
    TextView tvDoctor;
    @Bind(R.id.tv_dataname)
    TextView tvDataname;

    @Bind(R.id.ll_info)
    LinearLayout llInfo;

    String CLINICID = "10";//每次请求条数
    String EMPIID = "";
    private SearchAdapter searchAdapter;
    List<SearchResList.BodyBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        this.createPresenter(new HomePresenter(this, this));
        initView();
    }

    private void initView() {
        list = new ArrayList<>();
        searchAdapter = new SearchAdapter(this, list);
        pullLoadMoreRecyclerView.setAdapter(searchAdapter);
        //设置是否可以下拉刷新
        //mPullLoadMoreRecyclerView.setPullRefreshEnable(true);
        //设置是否可以上拉刷新
        //mPullLoadMoreRecyclerView.setPushRefreshEnable(false);
        //显示下拉刷新
        pullLoadMoreRecyclerView.setRefreshing(true);
        pullLoadMoreRecyclerView.setColorSchemeResources(R.color.colorgreen);
        //设置上拉刷新文字
        pullLoadMoreRecyclerView.setFooterViewText("正在加载");
//        设置上拉刷新文字颜色
        pullLoadMoreRecyclerView.setFooterViewTextColor(R.color.colorgreen);
        //设置加载更多背景色
        //mPullLoadMoreRecyclerView.setFooterViewBackgroundColor(R.color.colorBackground);
        pullLoadMoreRecyclerView.setLinearLayout();

        pullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        pullLoadMoreRecyclerView.setPullRefreshEnable(true);

        searchAdapter.setOnItemClickLitener(new ChildCaseAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int potison) {
                SearchResBean searchResBean = new SearchResBean();
                searchResBean.setCode("1000");
                SearchResBean.BodyBean body = new SearchResBean.BodyBean();
                body.setEMPIID(searchAdapter.getCurrentItem(potison).getEMPIID());
                body.setNAME((String) searchAdapter.getCurrentItem(potison).getNAME());
                body.setMEMBERCOST(searchAdapter.getCurrentItem(potison).getMEMBERCOST());
                body.setSEXCODE(searchAdapter.getCurrentItem(potison).getSEXCODE());
                body.setPERSONNAME(searchAdapter.getCurrentItem(potison).getPERSONNAME());
                body.setAGE(searchAdapter.getCurrentItem(potison).getAGE());
                body.setGRADENAME(searchAdapter.getCurrentItem(potison).getGRADENAME());
                body.setCREATETIME(searchAdapter.getCurrentItem(potison).getCREATETIME());
                body.setJZJG(searchAdapter.getCurrentItem(potison).getJZJG());
                body.setSTATUS(searchAdapter.getCurrentItem(potison).getSTATUS());
                body.setBIRTHDAY(searchAdapter.getCurrentItem(potison).getBIRTHDAY());
                body.setMANADOCTORID((String) searchAdapter.getCurrentItem(potison).getMANADOCTORID());
                body.setHEALTHCAREDATE(searchAdapter.getCurrentItem(potison).getHEALTHCAREDATE());
                body.setCARDNO(searchAdapter.getCurrentItem(potison).getCARDNO());
                body.setDATELINE(searchAdapter.getCurrentItem(potison).getDATELINE());
                body.setADVANCECOST(searchAdapter.getCurrentItem(potison).getADVANCECOST());
                body.setSEXNAME(searchAdapter.getCurrentItem(potison).getSEXNAME());
                body.setPHOTO(searchAdapter.getCurrentItem(potison).getPHOTO());
                searchResBean.setBody(body);
                Intent intent = new Intent();
                intent.setClass(HomeActivity.this, ChildInfoActivity.class);
                Gson gson = new Gson();
                String str = gson.toJson(searchResBean);
                intent.putExtra("SearchResBean", str);
                startActivity(intent);
            }
        });
        pullLoadMoreRecyclerView.setVisibility(View.VISIBLE);
        getData();

    }

    private void getData() {
        SearchListReq searchListReq = new SearchListReq();
        searchListReq.setMethod("execute");
        searchListReq.setServiceId("phis.mobileService");
        searchListReq.setServiceAction("getActivePersonByOrg");
        SearchListReq.BodyBean bodyBean = new SearchListReq.BodyBean();
        bodyBean.setCOUNT(CLINICID);
        if (!TextUtils.isEmpty(EMPIID)) {
            bodyBean.setEMPIID(EMPIID);
        }
        searchListReq.setBody(bodyBean);
        presenter.getActivePerson(searchListReq);
    }

    @Override
    public void hideLoading() {
        if (customProgress != null) {
            customProgress.dismiss();
        }
    }

    @Override
    public void showLoadingDialog() {
        customProgress = CustomProgress.show(this, "正在加载...", true, null);
    }

    @OnClick({R.id.btn_search, R.id.ll_info, R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_search:
                if (!TextUtils.isEmpty(edtSearch.getText().toString())) {
                    SearchBean searchBean = new SearchBean();
                    searchBean.setMethod("execute");
                    searchBean.setServiceId("phis.mobileService");
                    searchBean.setServiceAction("getMemberInfoByOrg");
                    SearchBean.BodyBean bodyBean = new SearchBean.BodyBean();
                    bodyBean.setCardNo(edtSearch.getText().toString());
                    searchBean.setBody(bodyBean);
                    presenter.toSearch(searchBean);
                    llInfo.setVisibility(View.VISIBLE);
                    pullLoadMoreRecyclerView.setVisibility(View.GONE);
                } else {
                    llInfo.setVisibility(View.GONE);
                    pullLoadMoreRecyclerView.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.ll_info:
                presenter.toInfoActivity();
                break;
            case R.id.ll_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        searchAdapter.clearData();
        EMPIID = "";
        getData();
    }

    @Override
    public void showInfo(SearchResBean resBean) {
        tvAccount.setText(resBean.getBody().getCARDNO());
        tvName.setText(resBean.getBody().getPERSONNAME());
        tvGender.setText(resBean.getBody().getSEXNAME());
        tvDoctor.setText(resBean.getBody().getBIRTHDAY());
        tvYue.setText(resBean.getBody().getADVANCECOST() + "");
        tvDataname.setText(resBean.getBody().getJZJG());
    }

    @Override
    public void showInfolist(SearchResList searchResBean) {
        list = new ArrayList<>();
        if (searchResBean.getBody().isEmpty()) {
            return;
        }
        list = searchResBean.getBody();
        searchAdapter.addAllData(list);

        if (list.size() > 0) {
            EMPIID = searchResBean.getBody().get(list.size() - 1).getEMPIID() + "";
        }
    }

    @Override
    public void stopRefresh() {
        pullLoadMoreRecyclerView.setRefreshing(false);
        pullLoadMoreRecyclerView.setPullLoadMoreCompleted();
    }

    @Override
    public void onLoadMore() {
        getData();
    }
}
